package com.yy.user.service.impl;

import com.yy.user.User;
import com.yy.user.jpa.UserRepository;
import com.yy.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * @author yangyao
 * @Description: UserService
 * @date 2018/8/23 14:55
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    @Cacheable
    public List<User> findAll() {
        System.out.println("从数据库查询+=================================");
        return userRepository.findAll();
    }

    @Override
    public Optional<User> findById(Long id) {
        return userRepository.findById(id);
    }

    @Override
    @CachePut
    public User save(User user) {
        return this.userRepository.save(user);
    }

    @Override
    @CachePut
    public void delete(Long id) {
        this.userRepository.delete(id);
    }
}
